﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoGo
{
    /// <summary>
    /// 篮子
    /// </summary>
    class Basket
    {
        //预定义篮子所在矩形
        public Rectangle Rectangle { get; set; }
        //篮子一次移动的距离
        private const int Step = 200;
        //切图变量
        private int i = 1;
        private DateTime ChangePicture { get; set; }

        //初始化篮子
        public Basket(Rectangle rec)
        {
            Rectangle = rec;
        }

        /// <summary>
        /// 绘图篮子
        /// </summary>
        /// <param name="g"></param>
        public void Draw(Graphics g)
        {
            //在到达换图时间的时候换图
            if (System.DateTime.Now > ChangePicture)
            {
                switch (i)
                {
                    case 1:
                        g.DrawImage(Properties.Resources.left15, Rectangle);
                        break;
                    case 2:
                        g.DrawImage(Properties.Resources.left7_5, Rectangle);
                        break;
                    case 3:
                        g.DrawImage(Properties.Resources.basket, Rectangle);
                        break;
                    case 4:
                        g.DrawImage(Properties.Resources.right7_5, Rectangle);
                        break;
                    case 5:
                        g.DrawImage(Properties.Resources.right15, Rectangle);
                        break;
                    case 6:
                        g.DrawImage(Properties.Resources.right7_5, Rectangle);
                        break;
                    case 7:
                        g.DrawImage(Properties.Resources.basket, Rectangle);
                        break;
                    case 8:
                        g.DrawImage(Properties.Resources.left7_5, Rectangle);
                        //循环
                        i = 0;
                        break;
                    default:
                        break;
                }
                i++;
                //换图的时间间隔为0.5秒
                ChangePicture = DateTime.Now.AddSeconds(0.5);
            }
            //在没有到换图时间时继续保持画之前的图
            else
            {
                switch (i - 1)
                {
                    case 0:
                        g.DrawImage(Properties.Resources.left15, Rectangle);
                        break;
                    case 1:
                        g.DrawImage(Properties.Resources.left7_5, Rectangle);
                        break;
                    case 2:
                        g.DrawImage(Properties.Resources.basket, Rectangle);
                        break;
                    case 3:
                        g.DrawImage(Properties.Resources.right7_5, Rectangle);
                        break;
                    case 4:
                        g.DrawImage(Properties.Resources.right15, Rectangle);
                        break;
                    case 5:
                        g.DrawImage(Properties.Resources.right7_5, Rectangle);
                        break;
                    case 6:
                        g.DrawImage(Properties.Resources.basket, Rectangle);
                        break;
                    case 7:
                        g.DrawImage(Properties.Resources.left7_5, Rectangle);
                        break;
                    default:
                        break;
                }
            }
        }

        //键盘控制篮子左右移动
        public void KeyDown(String key)
        {
            int newX = Rectangle.X;

            if (key == "A")
            {
                newX -= Step;
                //防止左出界
                if (newX < 55)
                    newX = 55;
            }

            if (key == "D")
            {
                newX += Step;
                //防止右出界
                if (newX > 855)
                    newX = 855;
            }

            Rectangle = new Rectangle(newX,Rectangle.Y,Rectangle.Width,Rectangle.Height);
        }
        
    }
}
